package com.xoa.service.supervision.impl;


import com.xoa.dao.supervision.SupFeedBackMapper;
import com.xoa.dao.supervision.SupFeedBackReplyMapper;
import com.xoa.dao.users.UsersMapper;
import com.xoa.model.enclosure.Attachment;
import com.xoa.model.supervision.SupFeedBack;
import com.xoa.model.supervision.SupFeedBackReply;
import com.xoa.model.users.Users;
import com.xoa.model.worldnews.News;
import com.xoa.service.supervision.SupFeedBackServise;
import com.xoa.util.CookiesUtil;
import com.xoa.util.DateFormat;
import com.xoa.util.GetAttachmentListUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.session.SessionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by gsb on 2017/8/7.
 */
@Service
public class SupFeedServiseImpl implements SupFeedBackServise{

    @Resource
    SupFeedBackMapper supFeedBackMapper;

    @Resource
    SupFeedBackReplyMapper supFeedBackReplyMapper;

    @Resource
    UsersMapper usersMapper;

    @Override
    @Transactional
    public ToJson<Object> getSupDetail(HttpServletRequest request,SupFeedBack supFeedBack,SupFeedBackReply supFeedBackReply){
        ToJson<Object> toJson=new ToJson<Object>(1,"error");
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        Users users= SessionUtils.getSessionInfo(request.getSession(),Users.class,new Users(),redisSessionCookie);
        supFeedBack.setCreaterId(users.getUserId());
        supFeedBack.setCreateTime(DateFormat.getCurrentTime());
        try{
            supFeedBack=supFeedBackMapper.getSupDetails(supFeedBack);
            //附件处理
            List<Attachment> attachmentList=new ArrayList<Attachment>();
            if(supFeedBack.getAttachmentName()!=null&&!"".equals(supFeedBack.getAttachmentName())){
                attachmentList= GetAttachmentListUtil.returnAttachment(supFeedBack.getAttachmentName(),supFeedBack.getAttachmentId(),"xoa" + (String) request.getSession().getAttribute("loginDateSouse"),GetAttachmentListUtil.SUPERVISION);
            }
            supFeedBack.setAttachmentList(attachmentList);
            List<Object> list=supFeedBackReplyMapper.selectContent(supFeedBack.getSid());
            if(supFeedBack!=null){
                if(!StringUtils.checkNull(supFeedBack.getCreateTime())){
                    supFeedBack.setCreateTime(supFeedBack.getCreateTime().substring(0,supFeedBack.getCreateTime().length()-2));
                }
                if(!StringUtils.checkNull(supFeedBack.getCreaterId())){
                    String name = usersMapper.getUsernameByUserId(supFeedBack.getCreaterId());
                    supFeedBack.setUserName(name);
                }
            }
            if(list.size()>0){
                toJson.setMsg("ok");
                toJson.setObj(list);
            }
            if(supFeedBack!=null){
                toJson.setObject(supFeedBack);
                toJson.setMsg("ok");
            }else {
                toJson.setMsg("没有详情信息");
            }
            toJson.setFlag(0);
        }catch (Exception e){
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }


    @Override
    public ToJson<SupFeedBack> deleteSupFeedBack(HttpServletRequest request,Integer sid){
        ToJson<SupFeedBack> toJson=new ToJson<SupFeedBack>(1,"error");
        try{
            supFeedBackMapper.deleteSupFeedBack(sid);
            if(sid!=null){
                toJson.setMsg("ok");
                toJson.setFlag(0);
            }else {
                toJson.setMsg("删除失败");
                toJson.setFlag(1);
            }
        }catch (Exception e){
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    @Override
    public ToJson<SupFeedBack> updateSupFeedBack(HttpServletRequest request,SupFeedBack supFeedBack){
       ToJson<SupFeedBack> toJson=new ToJson<SupFeedBack>(1,"error");
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        Users users= SessionUtils.getSessionInfo(request.getSession(),Users.class,new Users(),redisSessionCookie);
        supFeedBack.setCreaterId(users.getUserId());
       try{
           int a=supFeedBackMapper.updateSupFeedBack(supFeedBack);
           if(a>0){
               toJson.setMsg("ok");
               toJson.setFlag(0);
           }else {
               toJson.setMsg("更新失败");
               toJson.setFlag(1);
           }
       }catch (Exception e){
           e.printStackTrace();
           toJson.setMsg(e.getMessage());
       }
       return toJson;
    }


    @Override
    public ToJson<SupFeedBack> addSupFeedBack(HttpServletRequest request,SupFeedBack supFeedBack){
        ToJson<SupFeedBack> toJson=new ToJson<SupFeedBack>(1,"error");
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        Users users= SessionUtils.getSessionInfo(request.getSession(),Users.class,new Users(),redisSessionCookie);
        supFeedBack.setCreaterId(users.getUserId());
        supFeedBack.setCreateTime(DateFormat.getCurrentTime());
        try{
            int a=supFeedBackMapper.addSupFeedBack(supFeedBack);
            if(a>0){
                toJson.setMsg("ok");
                toJson.setFlag(0);
            }else {
                toJson.setMsg("发表失败");
                toJson.setFlag(1);
            }
        }catch (Exception e){
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    @Override
    public ToJson<SupFeedBack> getSupFeedBackList(HttpServletRequest request, SupFeedBack supFeedBack) {
        ToJson<SupFeedBack> toJson=new ToJson<SupFeedBack>(1,"error");
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        Users users= SessionUtils.getSessionInfo(request.getSession(),Users.class,new Users(),redisSessionCookie);
        if(supFeedBack.getCreateTime()==null&&supFeedBack.getCreateTime()=="0000-00-00"){
            supFeedBack.setCreateTime(DateFormat.getCurrentTime());
        }
        if(supFeedBack.getCreaterId()==null){
            supFeedBack.setCreaterId(users.getUserId());
        }
        try{
            List<SupFeedBack> list=supFeedBackMapper.getSupFeedBackList(supFeedBack);

            if(list.size()>0){
                toJson.setObj(list);
                toJson.setMsg("ok");
            }else {
                toJson.setMsg("空");
            }
            toJson.setFlag(0);
        }catch (Exception e){
            e.printStackTrace();
            toJson.setFlag(1);
        }
        return toJson;
    }

}
